package cqrtplm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.dto.RtCfgBomConfigurationDTO;
import cqrtplm.entity.RtCfgBomConfigurationDO;
import cqrtplm.service.RtCfgBomConfigurationService;
import cqrtplm.mapper.RtCfgBomConfigurationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 博
 * @description 针对表【RT_CFG_BOM_CONFIGURATION(配置管理-需求状态表-零部件装配bom更改规则表)】的数据库操作Service实现
 * @createDate 2025-09-12 18:34:15
 */
@Service
public class RtCfgBomConfigurationServiceImpl implements RtCfgBomConfigurationService{
    @Resource
    private RtCfgBomConfigurationMapper configurationMapper;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    /**
     * 根据主键查询
     * @param oid 主键
     * @return 配置信息
     */
    public RtCfgBomConfigurationDO selectByPrimaryKey(Long oid) {
        return configurationMapper.selectByPrimaryKey(oid);
    }

    /**
     * 查询所有记录
     * @return 配置信息列表
     */
    public List<RtCfgBomConfigurationDO> selectAll() {
        return configurationMapper.selectAll();
    }

    /**
     * 根据条件查询
     * @param configurationDTO 查询条件
     * @return 配置信息列表
     */
    @Override
    public List<RtCfgBomConfigurationDO> selectByCondition(RtCfgBomConfigurationDTO configurationDTO) {
        return configurationMapper.selectByCondition(configurationDTO);
    }

    /**
     * 根据条件分页查询
     * @param configurationDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageInfo<RtCfgBomConfigurationDO> selectByConditionWithPage(RtCfgBomConfigurationDTO configurationDTO) {
        PageHelper.startPage(configurationDTO.getPageNum(), configurationDTO.getPageSize());
        List<RtCfgBomConfigurationDO> list = configurationMapper.selectByCondition(configurationDTO);
        return new PageInfo<>(list);
    }

    /**
     * 插入记录
     * @param configurationDO 配置信息
     * @return 是否成功
     */
    public boolean insert(RtCfgBomConfigurationDO configurationDO) {
        UserDO user = TyAccountContext.getUser();
        configurationDO.setCreateuser(user.getName());
        configurationDO.setCreateuserid(user.getOid());
        // 设置创建时间
        configurationDO.setCreatetime(new Date());
        configurationDO.setOid(snowflakeIdComponent.getInstance().nextId());
        return configurationMapper.insert(configurationDO) > 0;
    }

    /**
     * 更新记录
     * @param configurationDO 配置信息
     * @return 是否成功
     */
    public boolean updateByPrimaryKey(RtCfgBomConfigurationDO configurationDO) {
        UserDO user = TyAccountContext.getUser();
        configurationDO.setUpdateuser(user.getName());
        configurationDO.setUpdateuserid(user.getOid());
        // 设置更新时间
        configurationDO.setUpdatetime(new Date());
        return configurationMapper.updateByPrimaryKey(configurationDO) > 0;
    }

    /**
     * 删除记录
     * @param oid 主键
     * @return 是否成功
     */
    public boolean deleteByPrimaryKey(Long oid) {
        return configurationMapper.deleteByPrimaryKey(oid) > 0;
    }

    @Override
    public List<String> listAllSegmNum() {
        return configurationMapper.listAllSegmNum();
    }
}
